﻿<%@ Control Language="C#" CodeFile="GridCommandEx.ascx.cs" Inherits="Shared_UserControl_GridCommandEx" %>
<telerik:RadCodeBlock runat="server" ID="RadCodeBlockCmd">
    <script>
        Sys.Application.add_load(function () {
            var btnDel = $find('<%=cDels.ClientID %>');<% if (cEdit.Visible){ %>
            var btnEdit = $find('<%=cEdit.ClientID %>');<%} %>
            var grid = $find("<%# DataBinder.Eval(Container, "OwnerGridID") %>");
            var tableView = grid.get_masterTableView();

            function deleteConfirm(s, e) {
                var callBackFunction = Function.createDelegate(s, function (argument) {
                    if (argument) {
                        this.click();
                    }
                });
                if (alertNoSelection(e))
                    radconfirm("确定要删除选择的记录？", callBackFunction, 280, 50, null, "删除确认");
                e.set_cancel(true);
            }

            function alertNoSelection(e) {
                if (tableView.get_selectedItems().length == 0) {
                    e.set_cancel(true);
                    radalert('请选择要操作的记录！', 280, 50, "警告！");
                    return false;
                }
                return true;
            }

            function sortExp(a, b) {
                return parseInt(a._itemIndexHierarchical)-parseInt(b._itemIndexHierarchical);
            }

            function setEnable(v) {
                btnDel.set_enabled(v);<% if (cEdit.Visible){ %>
                btnEdit.set_enabled(v);<%} %>
            }
            setEnable(false);
            btnDel.add_clicking(deleteConfirm);<%if (cEdit.Visible){ %>
            btnEdit.add_clicking(function(s, e) {
                if (alertNoSelection(e)) {
                    var row = tableView.get_selectedItems().sort(sortExp)[0];
                    <% if (!string.IsNullOrEmpty(onEditClicking)){ %>
                    <%=onEditClicking %>(row);
                    <%}else{ %>
                    tableView.editItem(row.get_element());<%} %>
                }
            });<%} %>
            grid.add_rowSelected(function() {
                setEnable(true);
            });
            grid.add_rowDeselected(function() {
                var selected = grid.get_selectedItems().length > 0;
                setEnable(selected);
            });
        });
    </script>
</telerik:RadCodeBlock>
<span class="pad3blk">
    <telerik:RadButton ID="cAdd" runat="server" Icon-PrimaryIconCssClass="icon-add"
        Text="添加" UseSubmitBehavior="false" ToolTip="添加" />
    <telerik:RadButton ID="cEdit" runat="server" Icon-PrimaryIconCssClass="icon-edit"
        Text="编辑" UseSubmitBehavior="false" AutoPostBack="false" ToolTip="编辑选择的单条记录" />
    <telerik:RadButton ID="cDels" runat="server" CommandName="DeleteSelected"
        Icon-PrimaryIconCssClass="icon-delete" Text="删除选中记录" UseSubmitBehavior="false" ToolTip="删除选中记录" />
    <telerik:RadButton ID="cRef" runat="server" CommandName="RebindGrid"
        Icon-PrimaryIconCssClass="icon-refresh" Text="刷新" UseSubmitBehavior="false" ToolTip="刷新" />
</span>